/**************************************************************************
 *
 * Copyright (c) 2015-2017  QIKU Company             　　　　　　　            
 *                 奇酷软件（深圳）有限公司  版权所有 2015-2017
 *
 * PROPRIETARY RIGHTS of QIKU Company are involved in the  　　　　　　     
 * subject matter of this material.  All manufacturing, reproduction, use, 
 * and sales rights pertaining to this subject matter are governed by the  
 * license agreement.  The recipient of this software implicitly accepts   
 * the terms of the license.                                               
 * 本软件文档资料是奇酷公司的资产,任何人士阅读和使用本资料必须获得                     
 * 相应的书面授权,承担保密责任和接受相应的法律约束.                                 
 *
 **************************************************************************/
package com.yw.android.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


/**
 * 字符串压缩解压接口
 *
 * @author yuanwei
 */
public class ZipUtils {

    /**
     * 使用GZIP压缩方式，对字节数组数据压缩
     *
     * @param plain 普通数据
     * @return 压缩后数据
     * @throws Exception 异常定义
     */
    public static byte[] gz(byte[] plain) throws Exception {
        ByteArrayInputStream input = null;
        ByteArrayOutputStream output = null;
        GZIPOutputStream gzOutput = null;

        try {
            input = new ByteArrayInputStream(plain);
            output = new ByteArrayOutputStream();
            gzOutput = new GZIPOutputStream(output);
            gzOutput.write(plain);
            gzOutput.close();
            return output.toByteArray();
        } catch (IOException e) {
            throw new Exception("compress(gzip) failed(IOException)", e);
        } catch (NullPointerException e) {
            throw new Exception("compress(gzip) failed(NullPointerException)", e);
        } finally {
            try {
                if (null != gzOutput) {
                    gzOutput.close();
                }
            } catch (Exception e) {
                LOG.e("close gzip output stream failed(Exception): "
                        + e.getMessage());
            }

            try {
                if (null != input) {
                    input.close();
                }
            } catch (Exception e) {
                LOG.e("close byte array input stream failed(Exception): "
                        + e.getMessage());
            }

            try {
                if (null != output) {
                    output.close();
                }
            } catch (Exception e) {
                LOG.e("close byte array output stream failed(Exception): "
                        + e.getMessage());
            }

            gzOutput = null;
            input = null;
            output = null;
        }
    }

    /**
     * 使用GZIP解压方式，对字节数组数据解压
     *
     * @param cipher 压缩数据
     * @return 解压后数据
     * @throws Exception 异常定义
     */
    public static byte[] ungz(byte[] cipher) throws Exception {
        ByteArrayInputStream input = null;
        ByteArrayOutputStream output = null;
        GZIPInputStream gzInput = null;
        int length = 0;
        byte[] buffer = new byte[1024];

        try {
            output = new ByteArrayOutputStream();
            input = new ByteArrayInputStream(cipher);
            gzInput = new GZIPInputStream(input);

            while ((length = gzInput.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }

            return output.toByteArray();
        } catch (IOException e) {
            throw new Exception("decompress(gzip) failed(IOException)", e);
        } catch (NullPointerException e) {
            throw new Exception("decompress(gzip) failed(NullPointerException)", e);
        } finally {
            try {
                if (null != gzInput) {
                    gzInput.close();
                }
            } catch (Exception e) {
                LOG.e("close gzip input stream failed(Exception): "
                        + e.getMessage());
            }

            try {
                if (null != input) {
                    input.close();
                }
            } catch (Exception e) {
                LOG.e("close byte array input stream failed(Exception): "
                        + e.getMessage());
            }

            try {
                if (null != output) {
                    output.close();
                }
            } catch (Exception e) {
                LOG.e("close byte array output stream failed(Exception): "
                        + e.getMessage());
            }

            gzInput = null;
            input = null;
            output = null;
        }
    }
}
